package com.shf.lease.web.admin.controller.apartment;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.shf.lease.entity.AttrKey;
import com.shf.lease.entity.AttrValue;
import com.shf.lease.result.Result;
import com.shf.lease.web.admin.controller.apartment.response.AttrKeyVo;
import com.shf.lease.web.admin.service.AttrKeyService;
import com.shf.lease.web.admin.service.AttrValueService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 叶枫
 * @date 2024/7/4.
 */

@Tag(name = "房间基本属性管理")
@RestController
@RequestMapping("/admin/attr")
public class AttrController {
    @Resource
    private AttrKeyService attrKeyService;
    @Resource
    private AttrValueService attrValueService;

    @Operation(summary = "查询房间基本属性列表")
    @GetMapping("/list")
    public Result<List<AttrKeyVo>> getAttrList(){
        Result<List<AttrKeyVo>> attrList = attrKeyService.getAttrList();
        return attrList;
    }

    @Operation(summary = "保存或更新房间基本属性名称")
    @PostMapping("/key/saveOrUpdate")
    public Result<?> saveOrUpdateAttrKey(@RequestBody AttrKey attrKey){
        return  attrKeyService.saveOrUpdate(attrKey) ? Result.ok() : Result.fail();
    }
    @Operation(summary = "保存或更新房间属性值")
    @PostMapping("/value/saveOrUpdate")
    public Result<?> saveOrUpdateAttrValue(@RequestBody AttrValue attrValue){
        return attrValueService.saveOrUpdate(attrValue) ? Result.ok() : Result.fail();
    }
    @Operation(summary = "删除房间基本属性名称")
    @DeleteMapping("/key/delete")
    public Result<?> deleteAttrKey(@RequestParam Long id){
        //删除房间名称
        attrKeyService.removeById(id);
        //删除房间属性名称下的所有值
        //构建查询条件
        LambdaQueryWrapper<AttrValue> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(id != null, AttrValue::getAttrKeyId, id);
        attrValueService.remove(queryWrapper);
        return Result.ok();
    }
    @Operation(summary = "删除房间属性值")
    @DeleteMapping("/value/delete")
    public Result<?> deleteAttrValue(@RequestParam Long id){
        return attrValueService.removeById(id) ? Result.ok() : Result.fail();
    }
}
